package com.scx.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scx.entity.Voucher;
import com.scx.mapper.VoucherMapper;
import com.scx.result.Result;
import com.scx.service.IVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements IVoucherService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryVoucher() {
        // 查询优惠券信息
        List<Voucher> vouchers = baseMapper.selectList(null);
        // 返回结果
        return Result.success(vouchers);
    }

    @Override
    @Transactional
    public void addSeckillVoucher(Voucher voucher) {
        // 保存优惠券
        save(voucher);
        stringRedisTemplate.opsForValue().set("seckill:stock:" + voucher.getId(), voucher.getStock().toString());
    }
}
